package com.example.micro.netty.server.tcpprotocal;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 实现自定义协议 编码器
 *
 * @author Code Farmer
 * @create 2021/3/8 12:03 上午
 */
public class MyMessageEncoder extends MessageToByteEncoder<MessageProtocal> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, MessageProtocal messageProtocal, ByteBuf byteBuf) throws Exception {
        System.out.println("MyMessageEncoder encoder 方法被调用");
        byteBuf.writeInt(messageProtocal.getLen());
        byteBuf.writeBytes(messageProtocal.getContent());
    }
}
